﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Windows.Controls.Ribbon;
using System.Windows;
using OODCRM.Common;
using System.Data.Linq;

namespace OODCRM
{
    public partial class MainWindow : RibbonWindow
    {

        private void btnCat_tabThemToChucDialog_Click(object sender, RoutedEventArgs e)
        {
            switch (dicDialogMode[CRMDialog.ThemToChuc])
            {
                case DialogMode.Them:
                    {
                        if (dicDialogData.ContainsKey(CRMDialog.ThemToChuc)
                            && dicDialogData[CRMDialog.ThemToChuc] != null)
                        {
                            var item = dicDialogData[CRMDialog.ThemToChuc] as TOCHUC;
                            if (item == null) break;

                            // update du lieu ko bind dc
                            var chuquan = cbxChuQuan_tabThemToChucDialog.SelectedItem as TOCHUC;
                            if (chuquan != null)
                                item.MaChuQuan = chuquan.MaHieu;
                            var chusohuu = cbxChuSoHuu_tabThemToChucDialog.SelectedItem as User;
                            if (chusohuu != null)
                                item.DOITUONG.User = chusohuu;
                            //var phanloai = cbxPhanLoai_tabThemToChucDialog.SelectedItem as PHANLOAITOCHUC;
                            //if (phanloai != null)
                            //{
                            //    var pl = item.CHITIETPHANLOAITOCHUCs.FirstOrDefault();
                            //    if (pl != null)
                            //        pl.PHANLOAITOCHUC = phanloai;
                            //    else
                            //    {
                            //        var ct = new CHITIETPHANLOAITOCHUC()
                            //            {
                            //                TOCHUC = item,
                            //                PHANLOAITOCHUC = phanloai,
                            //            };
                            //        item.CHITIETPHANLOAITOCHUCs.Add(ct);
                            //        dataContext.CHITIETPHANLOAITOCHUCs.InsertOnSubmit(ct);
                            //    }
                            //}

                            // cap nhat du lieu
                            try
                            {
                                dataContext.DIACHIs.InsertOnSubmit(item.DIACHI);
                                dataContext.DIACHIs.InsertOnSubmit(item.DIACHI1);
                                dataContext.TOCHUCs.InsertOnSubmit(item);
                                dataContext.SubmitChanges();

                                if (item != null)
                                    listToChuc.Add(item);
                                
                            }
                            catch (Exception ex)
                            {
                                //MessageBox.Show("Không thể thêm dữ liệu vừa điền! Kiểm tra lại hoặc Nạp lại dữ liệu");
                                MessageBox.Show(ex.Message);
                                dataContext = new CRMDataDataContext(Properties.Settings.Default.User_ConnectionString);
                               
                            }
                        }
                    }
                    break;

                case DialogMode.Sua:
                    {
                        var item = dicDialogData[CRMDialog.ThemToChuc] as TOCHUC;
                        if (item == null) break;

                        // update du lieu ko bind dc
                        var chuquan = cbxChuQuan_tabThemToChucDialog.SelectedItem as TOCHUC;
                        if (chuquan != null)
                            item.MaChuQuan = chuquan.MaHieu;
                        var chusohuu = cbxChuSoHuu_tabThemToChucDialog.SelectedItem as User;
                        if (chusohuu != null)
                            item.DOITUONG.User = chusohuu;
                        //var phanloai = cbxPhanLoai_tabThemToChucDialog.SelectedItem as PHANLOAITOCHUC;
                        //if (phanloai != null)
                        //{
                        //    var pl = item.CHITIETPHANLOAITOCHUCs.FirstOrDefault();
                        //    if (pl != null)
                        //    {
                        //        // Todo: viettp: ko gan lai duoc
                        //        pl.PHANLOAITOCHUC = phanloai;
                        //    }
                        //    else
                        //    {
                        //        var ct = new CHITIETPHANLOAITOCHUC()
                        //        {
                        //            TOCHUC = item,
                        //            PHANLOAITOCHUC = phanloai,
                        //        };
                        //        item.CHITIETPHANLOAITOCHUCs.Add(ct);
                        //        dataContext.CHITIETPHANLOAITOCHUCs.InsertOnSubmit(ct);
                        //    }
                        //}

                        // cap nhat du lieu
                        try
                        {
                            
                            dataContext.SubmitChanges();
                        }
                        catch (Exception ex)
                        {
                            //MessageBox.Show("Không thể thêm dữ liệu vừa điền! Kiểm tra lại hoặc Nạp lại dữ liệu");
                            MessageBox.Show(ex.Message);
                            dataContext = new CRMDataDataContext(Properties.Settings.Default.User_ConnectionString);
                        }
                    }
                    break;

                default:
                    break;
            }
            tabToChuc.IsSelected = true;
        }

        private void btnHuyBo_tabThemToChucDialog_Click(object sender, RoutedEventArgs e)
        {
            if (dicDialogMode[CRMDialog.ThemToChuc] == DialogMode.Sua)
            {
                var item = dicDialogData[CRMDialog.ThemToChuc] as TOCHUC;
                if (item != null)
                    dataContext.Refresh(RefreshMode.OverwriteCurrentValues, item);

                int index = listToChuc.IndexOf(item);
                listToChuc.RemoveAt(index);
                listToChuc.Insert(index, item);
            }

            tabToChuc.IsSelected = true;
        }


        #region Auto

        private void cbxQuocGiaTT_tabThemToChucDialog_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            if (cbxQuocGiaTT_tabThemToChucDialog.SelectedValue == null) return;
            var x = (from qh in ((VITRIDIALY)(cbxQuocGiaTT_tabThemToChucDialog.SelectedValue)).QUANHEVTDLs
                     select qh.VITRIDIALY1);
            if (x != null)
                cbxTinhThanhTT_tabThemToChucDialog.ItemsSource = x.ToList();
        }

        private void cbxTinhThanhTT_tabThemToChucDialog_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            if (cbxTinhThanhTT_tabThemToChucDialog.SelectedValue == null) return;
            var x = (from qh in ((VITRIDIALY)(cbxTinhThanhTT_tabThemToChucDialog.SelectedValue)).QUANHEVTDLs
                     select qh.VITRIDIALY1);
            if (x != null)
                cbxQuanHuyenTT_tabThemToChucDialog.ItemsSource = x.ToList();
        }

        private void cbxQuocGiaCH_tabThemToChucDialog_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            if (cbxQuocGiaCH_tabThemToChucDialog.SelectedValue == null) return;
            var x = (from qh in ((VITRIDIALY)(cbxQuocGiaTT_tabThemToChucDialog.SelectedValue)).QUANHEVTDLs
                     select qh.VITRIDIALY1);
            if (x != null)
                cbxTinhThanhCH_tabThemToChucDialog.ItemsSource = x.ToList();
        }

        private void cbxTinhThanhCH_tabThemToChucDialog_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            if (cbxTinhThanhCH_tabThemToChucDialog.SelectedValue == null) return;
            var x = (from qh in ((VITRIDIALY)(cbxTinhThanhTT_tabThemToChucDialog.SelectedValue)).QUANHEVTDLs
                     select qh.VITRIDIALY1);
            if (x != null)
                cbxQuanHuyenCH_tabThemToChucDialog.ItemsSource = x.ToList();
        }
        #endregion Auto


    }
}
